package org.zhb.querysql.sys.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.zhb.querysql.core.constant.RetCode;
import org.zhb.querysql.core.util.JsonReturn;
import org.zhb.querysql.model.Function;
import org.zhb.querysql.sys.service.FunctionService;

/**
 * @ClassName: Functioncontroller。
 * @Description: Function管理Controller
 * @author: zhb
 *
 */
@RestController
@RequestMapping("/function/")
public class FunctionController {
	@Autowired
	private FunctionService functionService;
	Logger log = LoggerFactory.getLogger(FunctionController.class);

	@RequestMapping("functionInfo")
	public ModelAndView roleInfo() {
		return new ModelAndView("shiro/function/functionInfo");
	}

	@RequestMapping("functionList")
	public JsonReturn functionList() {
		JsonReturn jsonReturn = new JsonReturn();
		Page<Function> page = functionService.selectModelByCondition(new PageRequest(0, 20));
		jsonReturn.setRetmsg(page);
		jsonReturn.setRetcode(RetCode.OK_CODE);
		return jsonReturn;
	}

	@RequestMapping("insert")
	public JsonReturn insert(Function function) {
		JsonReturn jsonReturn = new JsonReturn();
		try {
			functionService.save(function);
			log.info("保存成功!!!");
			jsonReturn.setRetcode(RetCode.OK_CODE);
			jsonReturn.setRetmsg("添加成功");
		} catch (Exception e) {
			jsonReturn.setRetcode(RetCode.ERROR_CODE);
			jsonReturn.setRetmsg("添加失败");
			log.info("保存成功!!!" + e.getMessage());
		}
		return jsonReturn;
	}

	@RequestMapping("selectFunctionById")
	public JsonReturn selectFunctionById(String id, HttpServletRequest request) {
		JsonReturn jsonReturn = new JsonReturn();
		try {
			Function function = functionService.findById(id);
			jsonReturn.setRetcode(RetCode.OK_CODE);
			jsonReturn.setRetmsg(function);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonReturn;
	}

	@RequestMapping("edit")
	public JsonReturn edit(Function function) {
		JsonReturn jsonReturn = new JsonReturn();
		try {
			functionService.update(function);
			log.info("更新成功!!!");
			jsonReturn.setRetcode(RetCode.OK_CODE);
			jsonReturn.setRetmsg("更新成功");
		} catch (Exception e) {
			log.info("更新失败!!!" + e.getMessage());
			jsonReturn.setRetcode(RetCode.ERROR_CODE);
			jsonReturn.setRetmsg("更新失败");
		}
		return jsonReturn;
	}

	@RequestMapping("delete")
	public JsonReturn delete(String id) {
		JsonReturn jsonReturn = new JsonReturn();
		try {
			functionService.delete(id);
			log.info("删除成功");
			jsonReturn.setRetcode(RetCode.OK_CODE);
			jsonReturn.setRetmsg("删除成功!");
		} catch (Exception e) {
			log.info("删除失败" + e.getMessage());
			jsonReturn.setRetcode(RetCode.ERROR_CODE);
			jsonReturn.setRetmsg("删除失败!");
			e.printStackTrace();
		}
		return jsonReturn;
	}
}